package com.example.controller;

import com.example.flow.sqrt.SqrtImpl;
import com.example.flow.sqrt.SqrtOutput;
import com.example.flow.sqrt.SqrtParams;
import com.neuronbit.businessflow.runtime.StepBeanProvider;
import com.neuronbit.businessflow.runtime.exec.BusinessFlow;
import com.neuronbit.businessflow.runtime.exec.ExecutedFlowInstance;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("/test")
public class TestController {
    @Autowired
    private StepBeanProvider stepBeanProvider;
    @Autowired
    private BusinessFlow businessFlow;

    @GetMapping("{a}")
    public SqrtOutput test(@Validated @PathVariable("a") String a) throws Exception {
        log.info("param of " + this.getClass().getName() + " is " + a);
        SqrtImpl sqrt = new SqrtImpl(stepBeanProvider, a);
        SqrtParams params = new SqrtParams(a, "b", new SqrtParams.Age(1));
        SqrtOutput sqrtOutput = sqrt.start(params);
        Future<SqrtOutput> future = sqrt.startAsync(params);
//        future.get();
//        future.get(1, TimeUnit.MICROSECONDS);
        log.info("out of " + this.getClass().getName() + " is " + sqrtOutput);
        return sqrtOutput;
    }

    @GetMapping("/x/{a}")
    public ExecutedFlowInstance testx(@PathVariable("a") String a) throws Exception {
        log.info("param of " + this.getClass().getName() + " is " + a);
        ExecutedFlowInstance instance = businessFlow.reRun(SqrtImpl.class, a);
        log.info("out of " + this.getClass().getName() + " is " + instance);
        return instance;
    }
}
